// Vue.js快速入门之四：axios安装和使用
// https://blog.csdn.net/jiciqiang/article/details/116195073


import axios from 'axios'
import { Message, Loading } from 'element-ui'
import store from '@/store/index'
import myFns from '@/utils/myFns'

// 配置全局数据请求类型
// axios.defaults.headers['Content-Type'] = "application/json;charset=utf-8";
axios.defaults.headers['Content-Type'] = "application/x-www-form-urlencoded;charset=UTF-8";

// 自定义实例
const request = axios.create({
    // 数据请求地址
	baseURL: myFns.apiUrl(),
	timeout: 30 * 1000
})

// 配置加载参数
let loadingOption = {
	text: "正在努力加载中...",
	background: "rgba(0, 0, 0, .5)"
}, loading;

// 请求拦截器
request.interceptors.request.use(config => {
	// 开始加载
	loading = Loading.service(loadingOption);
	
	// 追加访问令牌
	config.headers['token'] = store.getters.getToken;
	
	return config;
}, error => {
	loading.close();
	return Promise.reject(error);
});

// 响应拦截器
request.interceptors.response.use(response => {
	loading.close();
	if(response.status == 200){
		return response['data'];
	}
	return Promise.reject(response);
}, error => {
	loading.close();
	return Promise.reject(error);
});

export default request;